package java_1_8.interface_test;

import java.util.Arrays;

public class Student implements Comparable<Student>{//<>是一个泛型，就是类型的意思
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    //假设根据年龄大小比较
    @Override
    public int compareTo(Student o) {//年龄按升序排列，小的在前。若想要降序排列，<时return 1,>时return -1
        if(this.age==o.age){
            return 0;
        }else if(this.age<o.age){
            return -1;
        }else{
            return 1;
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public static void main(String[] args) {
        Student stu1=new Student("男1",18);
        Student stu2=new Student("男2",20);
        Student stu3=new Student("男3",30);
        Student[] students={stu3,stu1,stu2};
        //数组排序算法
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));
    }



}
